package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.online.infrastructure.mapper.PlanRelationMapper;
import com.todo.strategy.online.infrastructure.entity.po.PlanRelationPO;
import com.todo.strategy.online.infrastructure.repository.PlanRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PlanRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("planRelationRepository")
public class PlanRelationRepositoryImpl implements PlanRelationRepository {
    @Resource
    private PlanRelationMapper planRelationMapper;


    @Override
    public Boolean updatePlanRelationPOById(PlanRelationPO planRelationPO) {
        if (planRelationPO == null || planRelationPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(planRelationPO)) {
            return false;
        }
        return planRelationMapper.updatePlanRelationPOById(planRelationPO) > 0;
    }

    @Override
    public Boolean removePlanRelationPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return planRelationMapper.removePlanRelationPOById(id) > 0;
    }

    @Override
    public PlanRelationPO queryPlanRelationPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return planRelationMapper.queryPlanRelationPOById(id);
    }

    @Override
    public List<PlanRelationPO> queryPlanRelationListByBizId(Long businessId,Integer scene) {
        if (businessId == null || businessId < 0) {
            return Lists.newArrayList();
        }
        return planRelationMapper.queryPlanRelationListByBizId(businessId,scene);
    }

    @Override
    public Boolean insertPlanRelationPO(PlanRelationPO planRelationPO) {
        if (planRelationPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(planRelationPO)) {
            return false;
        }
        return planRelationMapper.insertPlanRelationPO(planRelationPO) > 0;
    }

    @Override
    public Boolean removePlanRelationByIdList(Long businessId, Integer businessScene, List<Long> idList) {
        if (businessId == null || businessId < 0 || businessScene == null || idList == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (CollectionUtils.isEmpty(idList)) {
            return true;
        }
        return planRelationMapper.removePlanRelationByIdList(businessId, businessScene, idList);
    }

    @Override
    public Boolean removePlanRelationByBizId(Long businessId, Integer type) {
        if (businessId == null || businessId < 0 || type == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return planRelationMapper.removePlanRelationByBizId(type, businessId) > 0;
    }

    @Override
    public Boolean insertPlanRelationList(List<PlanRelationPO> planRelationPOList) {
        if (CollectionUtils.isEmpty(planRelationPOList)) {
            return true;
        }
        return planRelationMapper.insertBatch(planRelationPOList) > 0;
    }

    private boolean baseFieldCheck(PlanRelationPO planRelationPO) {
        if (planRelationPO == null) {
            return false;
        }
        if (planRelationPO.getBusinessScene() == null || planRelationPO.getBusinessId() == null || planRelationPO.getTitle() == null) {
            log.warn("PlanRelationRepository#baseFieldCheck 核心字段校验失败 操作默认失败 planRelationPO：【{}】", JSONObject.toJSONString(planRelationPO));
            return false;
        }
        if (planRelationPO.getStartTime() == null || planRelationPO.getRelationName() == null || planRelationPO.getPlanDay() == null) {
            log.warn("PlanRelationRepository#baseFieldCheck 基础字段校验失败 操作默认失败 planRelationPO：【{}】", JSONObject.toJSONString(planRelationPO));
            return false;
        }
        return true;
    }
}
